﻿using System;
using System.Text;
using System.Data;
using System.Xml;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using Cosmos.Hardware.Plugs;
using Cosmos.IL2CPU;
using Cosmos.IL2CPU.X86;
using Cosmos.Kernel;
using Cosmos.Kernel.Plugs;
using Cosmos.Sys;
using Cosmos.Sys.FileSystem;
using Cosmos.Sys.Plugs;
using Cosmos.Compiler.Debug;
using Cosmos.Compiler.Builder;

/*
 * Platform OS Source Code
 * This code belongs to LeveL X and the developers who have
 * worked on it for them. Before using the source code please
 * ask Eamon N. Cusic Jr. (LeveL X Owner) by email at:
 * eamon.cusic@levelx.org
 * 
 * Platform OS is copyrighted to LeveL X as of 2012.
 * It uses the MIT License.
 * 
 * Written on 06/23/2012 at 8:35 AM EST (Eastern Standard Time)
 * Last Updated on 06/23/2012 at 8:35 AM EST
*/

using Platform_OS.Core.Components;

namespace Platform_OS.Core.Init
{
    class Init
    {
        #region Bash
        public static bool first = true;
        public static bool rootaccess = false;
        public static void InitBash()
        {
            #region Start
            if (first)
            {
                // Print Title and Version Information
                Kernel.DisplayHeader();

                // Only do this once
                first = false;
            }
            #endregion

            #region Input
            if (rootaccess == false)
            {
                Console.Write("$ ");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("root@PlatformOS ~");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("$ ");
            }
            var input = Console.ReadLine();
            #endregion

            #region Commands
            bool left = false;
            if (input == "help")
            {
                Console.WriteLine("Platform OS Bash Help");
                Console.WriteLine(" command and syntax : description ");
                Console.WriteLine("");
                Console.WriteLine(" clear : clear the screen ");
                Console.WriteLine(" echo [text] : echoes out [text] ");
                Console.WriteLine(" root : get root access ");
                Console.WriteLine(" gui : start the graphical user interface ");
                Console.WriteLine("");
            }
            else if (input == "clear")
            {
                Console.Clear();
                Kernel.DisplayHeader();
            }
            else if (input.Substring(0, 5) == "echo ") // Checks to see if an argument was supplied 
            {
                Console.WriteLine(input.Substring(5));
            }
            else if (input == "echo") // Check to see if no argument was supplied
            {
                Console.WriteLine("Invalid syntax: Proper syntax is echo [text]");
            }
            else if (input == "root")
            {
                Console.WriteLine(" ");
                Console.Write("Root Username: ");
                var user = Console.ReadLine();
                if (user == "root")
                {
                    Console.Write("Password: ");
                    var pass = Console.ReadLine();
                    if (pass == "")
                    {
                        rootaccess = true;
                        Console.WriteLine("Root access granted!");
                    }
                    else
                    {
                        Console.WriteLine("Incorrect Password! No root access granted.");
                    }
                }
                else
                {
                    Console.WriteLine("Incorrect Root Username! No root access granted.");
                }
            }
            else if (input == "gui")
            {
                left = true;
                InitGUI();
            }
            #endregion

            #region Restart
            // Start over if we haven't left yet
            if (left == false)
            {
                InitBash();
            }
            #endregion
        }
        #endregion

        #region GUI
        public static void InitGUI()
        {
            // Initialize the screen component
            Screen.Init();
            // Set color palettes
            Screen.SetColor(0, 0, 0, 0); // Black
            Screen.SetColor(1, 255, 255, 255); // White
            // Clear the Screen component with white
            Screen.Clear(Kernel.desktop);

            #region Mouse Variables
            Mouse.Initialize();
            uint x = (uint)Mouse.X / 2; // Mouse returns double the normal pixel location to smooth the movement
            uint y = (uint)Mouse.Y / 2;
            uint oc = 0;
            uint oc2 = 0;
            uint oc3 = 0;
            uint oc4 = 0;
            uint oc5 = 0;
            uint oc6 = 0;
            uint oc7 = 0;
            uint oc8 = 0;
            uint oc9 = 0;
            uint oc10 = 0;
            uint oc11 = 0;
            uint oc12 = 0;
            uint oc13 = 0;
            uint oc14 = 0;
            uint oc15 = 0;
            uint oc16 = 0;
            uint oc17 = 0;
            uint oc18 = 0;
            uint oc19 = 0;
            uint oc20 = 0;
            uint oc21 = 0;
            uint oc22 = 0;
            uint oc23 = 0;
            uint oc24 = 0;
            uint oc25 = 0;
            uint mx = (uint)Mouse.X / 2; // 2 variables are needed to make sure the mouse redraws correctly
            uint my = (uint)Mouse.Y / 2;
            bool mousefirst = true;
            x = (uint)Mouse.X / 2; // Mouse returns double the normal pixel location to smooth the movement
            y = (uint)Mouse.Y / 2;
            #endregion

            #region Loop
            while (true)
            {
                #region Mouse Data
                mx = (uint)Mouse.X / 2;
                my = (uint)Mouse.Y / 2;

                if (mousefirst)
                {
                    oc = Screen.GetPixel(x, y);
                    oc2 = Screen.GetPixel(x + 1, y);
                    oc3 = Screen.GetPixel(x + 2, y);
                    oc4 = Screen.GetPixel(x + 3, y);
                    oc5 = Screen.GetPixel(x + 4, y);
                    oc6 = Screen.GetPixel(x, y + 1);
                    oc7 = Screen.GetPixel(x, y + 2);
                    oc8 = Screen.GetPixel(x, y + 3);
                    oc9 = Screen.GetPixel(x, y + 4);
                    oc10 = Screen.GetPixel(x + 1, y + 1);
                    oc11 = Screen.GetPixel(x + 1, y + 2);
                    oc12 = Screen.GetPixel(x + 1, y + 3);
                    oc13 = Screen.GetPixel(x + 1, y + 4);
                    oc14 = Screen.GetPixel(x + 2, y + 1);
                    oc15 = Screen.GetPixel(x + 2, y + 2);
                    oc16 = Screen.GetPixel(x + 2, y + 3);
                    oc17 = Screen.GetPixel(x + 2, y + 4);
                    oc18 = Screen.GetPixel(x + 3, y + 1);
                    oc19 = Screen.GetPixel(x + 3, y + 2);
                    oc20 = Screen.GetPixel(x + 3, y + 3);
                    oc21 = Screen.GetPixel(x + 3, y + 4);
                    oc22 = Screen.GetPixel(x + 4, y + 1);
                    oc23 = Screen.GetPixel(x + 4, y + 2);
                    oc24 = Screen.GetPixel(x + 4, y + 3);
                    oc25 = Screen.GetPixel(x + 4, y + 4);
                    mousefirst = false;
                }
                if (mx != x || my != y)
                {
                    Screen.SetPixel(x, y, oc);
                    Screen.SetPixel(x + 1, y, oc2);
                    Screen.SetPixel(x + 2, y, oc3);
                    Screen.SetPixel(x + 3, y, oc4);
                    Screen.SetPixel(x + 4, y, oc5);
                    Screen.SetPixel(x, y + 1, oc6);
                    Screen.SetPixel(x, y + 2, oc7);
                    Screen.SetPixel(x, y + 3, oc8);
                    Screen.SetPixel(x, y + 4, oc9);
                    Screen.SetPixel(x + 1, y + 1, oc10);
                    Screen.SetPixel(x + 1, y + 2, oc11);
                    Screen.SetPixel(x + 1, y + 3, oc12);
                    Screen.SetPixel(x + 1, y + 4, oc13);
                    Screen.SetPixel(x + 2, y + 1, oc14);
                    Screen.SetPixel(x + 2, y + 2, oc15);
                    Screen.SetPixel(x + 2, y + 3, oc16);
                    Screen.SetPixel(x + 2, y + 4, oc17);
                    Screen.SetPixel(x + 3, y + 1, oc18);
                    Screen.SetPixel(x + 3, y + 2, oc19);
                    Screen.SetPixel(x + 3, y + 3, oc20);
                    Screen.SetPixel(x + 3, y + 4, oc21);
                    Screen.SetPixel(x + 4, y + 1, oc22);
                    Screen.SetPixel(x + 4, y + 2, oc23);
                    Screen.SetPixel(x + 4, y + 3, oc24);
                    Screen.SetPixel(x + 4, y + 4, oc25);
                    x = mx;
                    y = my;
                    oc = Screen.GetPixel(x, y);
                    oc2 = Screen.GetPixel(x + 1, y);
                    oc3 = Screen.GetPixel(x + 2, y);
                    oc4 = Screen.GetPixel(x + 3, y);
                    oc5 = Screen.GetPixel(x + 4, y);
                    oc6 = Screen.GetPixel(x, y + 1);
                    oc7 = Screen.GetPixel(x, y + 2);
                    oc8 = Screen.GetPixel(x, y + 3);
                    oc9 = Screen.GetPixel(x, y + 4);
                    oc10 = Screen.GetPixel(x + 1, y + 1);
                    oc11 = Screen.GetPixel(x + 1, y + 2);
                    oc12 = Screen.GetPixel(x + 1, y + 3);
                    oc13 = Screen.GetPixel(x + 1, y + 4);
                    oc14 = Screen.GetPixel(x + 2, y + 1);
                    oc15 = Screen.GetPixel(x + 2, y + 2);
                    oc16 = Screen.GetPixel(x + 2, y + 3);
                    oc17 = Screen.GetPixel(x + 2, y + 4);
                    oc18 = Screen.GetPixel(x + 3, y + 1);
                    oc19 = Screen.GetPixel(x + 3, y + 2);
                    oc20 = Screen.GetPixel(x + 3, y + 3);
                    oc21 = Screen.GetPixel(x + 3, y + 4);
                    oc22 = Screen.GetPixel(x + 4, y + 1);
                    oc23 = Screen.GetPixel(x + 4, y + 2);
                    oc24 = Screen.GetPixel(x + 4, y + 3);
                    oc25 = Screen.GetPixel(x + 4, y + 4);

                }
                Screen.SetPixel(mx, my, (uint)Kernel.black);
                Screen.SetPixel(mx + 1, my, (uint)Kernel.black);
                Screen.SetPixel(mx + 2, my, (uint)Kernel.black);
                Screen.SetPixel(mx + 3, my, (uint)Kernel.black);
                Screen.SetPixel(mx + 4, my, (uint)Kernel.black);
                Screen.SetPixel(mx, my + 1, (uint)Kernel.black);
                Screen.SetPixel(mx, my + 2, (uint)Kernel.black);
                Screen.SetPixel(mx, my + 3, (uint)Kernel.black);
                Screen.SetPixel(mx, my + 4, (uint)Kernel.black);
                Screen.SetPixel(mx + 1, my + 1, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 1, my + 2, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 1, my + 3, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 1, my + 4, (uint)Kernel.black);
                Screen.SetPixel(mx + 2, my + 1, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 2, my + 2, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 2, my + 3, (uint)Kernel.black);
                Screen.SetPixel(mx + 2, my + 4, (uint)Kernel.black);
                Screen.SetPixel(mx + 3, my + 1, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 3, my + 2, (uint)Kernel.black);
                Screen.SetPixel(mx + 3, my + 3, (uint)Kernel.mouse);
                Screen.SetPixel(mx + 3, my + 4, (uint)Kernel.black);
                Screen.SetPixel(mx + 4, my + 1, (uint)Kernel.black);
                Screen.SetPixel(mx + 4, my + 2, (uint)Kernel.black);
                Screen.SetPixel(mx + 4, my + 3, (uint)Kernel.black);
                Screen.SetPixel(mx + 4, my + 4, (uint)Kernel.black);
                #endregion
            }
            #endregion
        }
        #endregion
    }
}
